const db=wx.cloud.database()
const pics=db.collection('pics')
const votes=db.collection('votes')
const $=db.command.aggregate
Page({
    data:{
        maxvote:3
    },
    async onLoad(e){
        let res=await pics.get()
        let plist=res.data
        res=await votes.get()
        let vlist=res.data
        plist.forEach(v=>{
            v.count=0
            vlist.forEach(vv=>{
                if(vv.fileid==v.fileid){
                    v.count++
                }
            })
        })
        res=await wx.cloud.callFunction({
            name:'login'
        })
        console.log('login:',res)
        let openid=res.result.openid
        let voted=false
        let votesum=0
        let today=new Date()
        today=today.toJSON().slice(0,10)
        console.log('today:',today)
        vlist.forEach(v=>{
            if(v._openid==openid&&v.date.toJSON().slice(0,10)==today){
                votesum++
            }
        })
        this.setData({
            plist,vlist,votesum
        })
        if(e.fileid){//是否有票数信息
            let current=plist.findIndex(v=>{
                return v.fileid==e.fileid
            })
            this.setTitleBar(current)
            this.setData({
                current
            })
        }else{
            this.setTitleBar(0)
        }
    },
    async long(){
       let res=await wx.chooseImage({
         count: 1,
       })
       console.log(res)
       let filename=res.tempFilePaths[0]
       let purfilename=filename.split('/').slice(-1)[0]
       res=await wx.cloud.uploadFile({
           cloudPath:'work15/'+purfilename,
           filePath:filename
       })
       let fileid=res.fileID
       console.log('fileid')
       console.log(fileid)
       res=await pics.add({
           data:{
               fileid
           }
       })
       if(res.errMsg.indexOf('ok')>-1){
           wx.showToast({
             title: '图片上传成功',
           })
           setTimeout(() => {
            wx.reLaunch({
                url: '/pages/work15/index?fileid='+fileid,
              })
        }, 1000);
       }
    },
    async tap(e){
        if(this.data.votesum>=this.data.maxvote){
            wx.showToast({
              title: '超过投票次数',
              icon:'error'
            })
            return
        }
        console.log(e)
        let fileid=e.currentTarget.dataset.id
        let res=await votes.add({
            data:{
                fileid,
                date:db.serverDate()
            }
        })
        console.log(res)
        if(res.errMsg.indexOf('ok')>-1){
            let s= this.data.maxvote-this.data.votesum-1
            await wx.showToast({
              title: '还有'+s+'次投票机会',
            })
            setTimeout(() => {
                wx.reLaunch({//投票后重载页面
                    url: '/pages/work15/index?fileid='+fileid,
                  })
            }, 1000);
        }else{
            await wx.showToast({
              title: '投票失败',
              icon:'error'
            })
        }
    },
    setTitleBar(e){//titlebar显示票数
        wx.setNavigationBarTitle({
          title:e+1+'/'+this.data.plist.length+' '+this.data.plist[e].count+'票'
        })
    },
    change(e){
        console.log('current:',e)
        this.setTitleBar(e.detail.current)
    }
})